package com.sgb.knowledgeshare.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.ShareConfig;
import com.sgb.knowledgeshare.entity.VideoCommentListEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by：冯齐 on 2021/2/2 11:08
 * Description： 直评弹窗实现
 */
public class CommentSendDialog {

    public CommentSendDialog(Activity context, VideoBean videoBean) {
        final Dialog dialog = new Dialog(context, R.style.RoundCornerDialog);
        //2、设置布局
        View view = View.inflate(context, R.layout.comment_send_dialog, null);
        EditText edComments = view.findViewById(R.id.ed_comment);//文本输入框
        TextView commentSend = view.findViewById(R.id.comment_send);//发送
        LinearLayout llcommentSend = view.findViewById(R.id.ll_comment_send);//发送
        dialog.setContentView(view);
        edComments.setText("");
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        shoftkData(edComments, llcommentSend);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        //发送评论
        commentSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edComments.getText().toString().trim())) {
                    MToast.showToast(context, "请输入评论内容信息!");
                    return;
                }
                sendComment(context, videoBean, edComments.getText().toString().trim(), dialog, llcommentSend);
            }
        });
    }

    private void shoftkData(EditText edComments, LinearLayout llcommentSend) {
        edComments.setFocusableInTouchMode(true);
        edComments.requestFocus();
        edComments.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) edComments.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(edComments, 0);
            }
        }, 100);
    }

    private void sendComment(Context context, VideoBean videoBean, String comment, Dialog dialog, LinearLayout llcommentSend) {
        DialogHelper.showProgressDialog(context, null, "提交中...", 0, true, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("videoId", videoBean.getVideoId());
        map.put("content", comment);
        map.put("parentId", 0);
        map.put("receiveUser", videoBean.getCreateUser());
        map.put("replyId", 0);
        ShareConfig.getInstance().getApi().addVideoComment(map).enqueue(new Callback<BaseEntity<VideoCommentListEntity.RecordsBeanX>>() {
            @Override
            public void onResponse(Call<BaseEntity<VideoCommentListEntity.RecordsBeanX>> call, Response<BaseEntity<VideoCommentListEntity.RecordsBeanX>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(ShareConfig.RESPONSE_OK)) {
                    MToast.showToast(context, "评论成功！");
                    dialog.dismiss();
                    commentsucceed.OnCommentNumber();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<VideoCommentListEntity.RecordsBeanX>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private CommentSucceed commentsucceed;

    public void setCommentsucceed(CommentSucceed commentsucceed) {
        this.commentsucceed = commentsucceed;
    }

    public interface CommentSucceed {
        void OnCommentNumber();
    }
}
